package vcp.application;

import java.net.InetAddress;

import vcp.client.GUI.events.ConnectionRequestEvent;
import vcp.client.interfaces.listeners.IConnectionListener;
import vcp.server.VCPServer;
import vcp.server.GUI.ServerView;
import vcp.server.utility.Constants;
import vcp.server.utility.timedecision.TimeDecision;

/**
 * Server application start the server
 *  
 * @author <p>
 *         VCP team
 *         </p>
 */
public class VCPServerApp implements
							IConnectionListener
{
	private ServerView view;

	/**
	 * @param args
	 *            Server application entry point
	 */
	public static void main ( String[] args )
	{
		new VCPServerApp().createAndRunServer();
	}

	/**
	 * Create new server and run it
	 */
	private void createAndRunServer ()
	{
		this.view = new ServerView(this);
	}

	@Override
	public void onConnectionRequest ( ConnectionRequestEvent connectionEvent )
	{
		Integer port = connectionEvent.getPort();
		try
		{
			VCPServer server = new VCPServer(port, TimeDecision.createTimeDecision(this.view.getSelectedTimeChoose()));
			server.listen();

			this.view.setIPLabel(InetAddress.getLocalHost().getHostAddress());
			this.view.disableConnectionOption();
		}
		catch ( Exception exception )
		{
			this.view.showError(Constants.PORT_IS_BUTY);
		}
	}

	@Override
	public void onConnectionLost ( String lostReason )
	{
	}

}
